import * as API from '@/api/dataBoard'
import { ref, reactive, onBeforeMount, watch } from 'vue'
import dayjs from 'dayjs'
import { start } from 'nprogress'

const dicts = {
  left1: {
    dictCode: 'large_screen_left',
    itemName: '用电量',
  },
  left2: {
    dictCode: 'large_screen_left',
    itemName: '自来水用量及单耗',
  },
  left3: {
    dictCode: 'large_screen_left',
    itemName: 'N2用量',
  },
  left4: {
    dictCode: 'large_screen_left',
    itemName: '天然气用量及单耗',
  },
  left5: {
    dictCode: 'large_screen_left',
    itemName: '化学品单耗',
  },
  left6: {
    dictCode: 'large_screen_left',
    itemName: '电能单耗及产量',
  },
  middleTop: {
    dictCode: 'large_screen_energy_consumption_proportion_of_each_branch_factory',
    itemName: '各分厂能耗占比',
    dataType: 1
  },
  middle1: {
    dictCode: 'da_ping_energy_consumption_of_each_branch_factory',
    itemName: 'ARRAY',
    dataType: 1
  },
  middle2: {
    dictCode: 'CIM_input_volume_and_output_volume',
    itemName: 'ARRAY',
  },
  middle3: {
    dictCode: 'CIM_unit_consumption',
    itemName: 'ARRAY',
  },
  middle4: {
    dictCode: 'da_ping_energy_consumption_of_each_branch_factory',
    itemName: 'CF',
    dataType: 1
  },
  middle5: {
    dictCode: 'CIM_input_volume_and_output_volume',
    itemName: 'CF',
  },
  middle6: {
    dictCode: 'CIM_unit_consumption',
    itemName: 'CF',
  },
  middle7: {
    dictCode: 'da_ping_energy_consumption_of_each_branch_factory',
    itemName: 'CELL',
    dataType: 1
  },
  middle8: {
    dictCode: 'CIM_input_volume_and_output_volume',
    itemName: 'CELL',
  },
  middle9: {
    dictCode: 'CIM_unit_consumption',
    itemName: 'CELL',
  },
  right1: {
    dictCode: 'large_screen_right',
    itemName: 'COP',
  },
  right2: {
    dictCode: 'large_screen_right',
    itemName: '压缩空气',
  },
  right3: {
    dictCode: 'large_screen_right',
    itemName: '纯废水',
  },
  right4: {
    dictCode: 'large_screen_right',
    itemName: '动力',
  },
}

const getQueryParams = (dictKey: any) => {
  console.log(dictKey, 'dictKey')
  // if (dictKey == 'left6') {
  //   return {
  //     ...dicts[dictKey],

  //     queryType: 'sevenDay',
  //     endDay: dayjs().format('YYYY-MM-DD'),
  //   }
  // } else {
    return {
      ...dicts[dictKey],
      queryType: 'custom',
      startDay: dayjs().subtract(7, 'day').format('YYYY-MM-DD'),
      endDay: dayjs().subtract(1, 'day').format('YYYY-MM-DD'),
    }
  // }
}

export default () => {
  const data = reactive({
    left1: null,
    left2: null,
    left3: null,
    left4: null,
    left5: null,
    left6: null,
    middleTop: null,
    middle1: null,
    middle2: null,
    middle3: null,
    middle4: null,
    middle5: null,
    middle6: null,
    middle7: null,
    middle8: null,
    middle9: null,
    right1: null,
    right2: null,
    right3: null,
    right4: null,
  })
  const request = async (key, api, params, cb?: any) => {
    const res = await api(params)
    if (res) {
      if (cb) {
        cb(res)
      } else {
        data[key] = res
      }
    }
  }
  const getData = () => {
    //left
    request('left1', API.getRight4, getQueryParams('left1'))
    request('left2', API.getRight2, getQueryParams('left2'))
    request('left3', API.getRight2, getQueryParams('left3'))
    request('left4', API.getLeft4, getQueryParams('left4'))
    request('left5', API.getLeft4, getQueryParams('left5'))
    request('left6', API.getRight3, getQueryParams('left6'))
    //middle
    request('middleTop', API.getPie, getQueryParams('middleTop'))
    request('middle1', API.getMiddleBar, getQueryParams('middle1'), (res) => {
      data.middle1 = res.ratid
    })
    request('middle2', API.childData, getQueryParams('middle2'))
    request('middle3', API.comprehensiveSingleCost, getQueryParams('middle3'))
    request('middle4', API.getMiddleBar, getQueryParams('middle4'), (res) => {
      data.middle4 = res.ratid
    })
    request('middle5', API.childData, getQueryParams('middle5'))
    request('middle6', API.comprehensiveSingleCost, getQueryParams('middle6'))
    request('middle7', API.getMiddleBar, getQueryParams('middle7'), (res) => {
      data.middle7 = res.ratid
    })
    request('middle8', API.childData, getQueryParams('middle8'))
    request('middle9', API.comprehensiveSingleCost, getQueryParams('middle9'))
    //right
    request('right1', API.getRight1, getQueryParams('right1'))
    request('right2', API.getRight5, getQueryParams('right2'))
    request('right3', API.getRight3, getQueryParams('right3'))
    request('right4', API.getRight4, getQueryParams('right4'))
  }
  onBeforeMount(() => {
    getData()
  })
  return {
    data,
  }
}
